<?php
use yii\helpers\Html;
use kartik\form\ActiveForm;
use kartik\file\FileInput;
use yii\helpers\Url;

$this->title = Yii::t('app', 'Profile');
$this->params['breadcrumbs'][] = $this->title;
?>

<div class="row profile">
    <?php $form = ActiveForm::begin(['id' => 'form-profile','options'=> ['enctype' => 'multipart/form-data']]); ?>
    <div class="col-md-12">
        <div class="flex flex-end align-center">
            <?= Html::submitButton('<i class="fa fa-save"> </i> '.Yii::t('app', 'Save'), ['class' =>  'btn btn-primary']) ?>
        </div>
        <div class="space10"></div>

        <div class="box box-primary">
            <div class="box-header">
                <h3><?=Yii::t('app','Basic Information')?></h3>
                <hr>
            </div>
            <div class="box-body page-section singleUpload">
                <?php
                echo $form->field($model, 'file')->label(Yii::t('app','Avatar'))
                    ->widget(FileInput::classname(),[
                        'name' => 'file',
                        'options'=>['multiple'=>false,'accept' => 'image/*'],
                        'pluginOptions' => [
                            'previewFileType' => 'image',
                            'initialPreview'  => $model->avatar?FILE_PATH.$model->avatar->path:false,
                            'initialPreviewAsData' => $model->avatar?true:false,
                            'allowedFileTypes' => ['image'],
//                            'minFileCount' => 1,
//                            'maxFileCount' => 1,
                            'showPreview' => $model->avatar?true:false,
                            'showUpload' => false,
                            'showRemove' => false,
                            'overwriteInitial'=>true,
                            'showCaption' => true,

                            'fileActionSettings' => [
                                // 设置具体图片的查看属性为false,默认为true
                                'showNew'=> false,
                                'showDrag' => false,
                                'showZoom' => false,
                                // 设置具体图片的上传属性为true,默认为true
                                'showUpload' => false,
                                // 设置具体图片的移除属性为true,默认为true
                                'showRemove' => false,
                            ],
                        ],
                        'pluginEvents' => [
                            // 上传成功后的回调方法，需要的可查看data后再做具体操作，一般不需要设置
                            "fileuploaded" => "function (event, data, id, index) {
                                  console.log('--上传--');
                                  console.log(data);
                            }",
                            "filesorted" => "function (event, data, id, index) {
                                  console.log('--排序--');
                                  console.log(data.stack);
                            }",
                            "filepreremove" => "function (event, data, id, index) {
                                  console.log('preremove')
                            }",
                            "fileremoved" => "function (event, data, id, index) {
                                  console.log('preremove')
                            }",
                            "fileclear" => "function (event, data, id, index) {
                                  if(data!=='undefined'){
                                    console.log('fileclear')
                                    $('.galleryFiles').val('')
                                  }
                            }"
                        ],
                    ]);
                ?>

                <?= $form->field($model, 'realname')->textInput()->label(Yii::t('app','Realname')) ?>
                <?= $form->field($model, 'email')->textInput()->label(Yii::t('app','Mobile')) ?>
                <?= $form->field($model, 'mobile')->textInput()->label(Yii::t('app','Email')) ?>
            </div>
        </div>
    </div>

    <?php ActiveForm::end(); ?>
</div>

<script src="../js/jquery-2.2.3.min.js?v=1.0"></script>
<script>
    //删除文件
    function deleteFile(id,that){
        $.ajax({
            url: 'delete-file?id='+id, //失败测试
            dataType: 'json',
            type: "get",
            async: false,
            beforeSend: function () {
                console.log('-----  start delete -----')
                ymz.jq_loading.show({text:"正在删除", is_cover: 1});
            },
            success: function(data){
                <!--                console.log(data)-->
                // console.log(data)
                if(data.data == 'succ'){
                    $(that).parent().hide();
                    $('.file.update').removeClass('update');
                    // var hiddenObj = $(that).parent().parent().children('input');
                    // console.log("hiddenObj: "+hiddenObj.val())
                    // hiddenObj.val(0);
                    // console.log("hiddenObj: "+hiddenObj.val())
                }
            },
            error: function (e) {
                console.log(e.status);
                ymz.jq_toast({text:"删除出错", type: "success", sec: 1});
            },
            complete: function (e){
                console.log('-----  end delete -----')
                ymz.jq_loading.hide();
            }
        });
    }
</script>

